////////////////////////////////////////////////////////////////////////////////
// Copyright 2006 CORE Solutions Limited
// All rights reserved.
// PROPRIETARY AND CONFIDENTIAL INFORMATION.
// Use is subject to license terms.
//
// FUNCTION ID      :   REMARK
// AUTHOR           :   Ric Yik
// DATE CREATE      :   2006/01/05
// DESCRIPTION      :   a MAINTENANCE ACTION file for the function, REMARK
//                  :
//                  :
//                  :
//
// MODIFICATION HISTORY:
// DATE         AUTHOR          VERSION NO.         COMMENT
// -----------  --------------  ------------------  -------------------
// 2006/01/05   Ric Yik         1.0                 source is generated.
//
//
//
////////////////////////////////////////////////////////////////////////////////

package com.erp.app.setup.remark.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.erp.app.common.ProjectConstants;
import com.erp.app.setup.remark.pk.RemarkHdrPk;
import com.erp.app.setup.remark.schema.RemarkHdrSchema;
import com.erp.app.setup.remark.vo.RemarkHdrVo;
import com.erp.app.util.web.WebUtil;
import com.erp.framework.controller.action.SimpleMaintStyleAction;
import com.erp.framework.model.pk.SimpleBasePk;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.util.logger.Logger;
import com.erp.framework.util.user.User;

/**
* A <strong>Maintenance Action</strong> is an adapter between the contents of an incoming
* HTTP request and the corresponding business logic that should be executed to
* process this request.  The controller (ActionServlet) will select an
* appropriate Action for each request.
* The main purpose for this action class is to handle the user document maintenance events
* from the web browsers.
*
* @author          Ric Yik
* @version 1.0     2006/01/05
* @since           FrameWork 1.0
*
*/
public class RemarkMaintenanceAction extends SimpleMaintStyleAction {

	////////////////////////////////////////////////////////////////////////////////
	// CONSTANTS DEFINITION
	////////////////////////////////////////////////////////////////////////////////

    protected static final String FIELD_REMARKMASTERDESC = "RemarkMasterDesc";
    protected static final String FIELD_DTLREMARK = "DtlRemark";
    protected static final String ACTION_PEDIT = "pEdit";
    protected static final String STATUS_PSET = "pSet";

	protected static final String P_EDIT_DTL_REMARK = "pEditDtlRemark";
    public static final int TAB_IDX_DTL = 0;
    public static final int TAB_IDX_HDR = -1;

    public static final String ACTION_CHANGE_FOLDER_HDR = ACTION_CHANGE_FOLDER + "Hdr";
	public static final String ACTION_CHANGE_FOLDER_DTL = ACTION_CHANGE_FOLDER + "Dtl";

	////////////////////////////////////////////////////////////////////////////////
	// GLOBAL VARIABLES DEFINITION
	////////////////////////////////////////////////////////////////////////////////



	////////////////////////////////////////////////////////////////////////////////
	// LOGICAL AND BUSINESS METHODS
	////////////////////////////////////////////////////////////////////////////////

	public ActionForward handlePageEvent(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		RemarkMaintenanceForm form = (RemarkMaintenanceForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {
			if (!isHandled(request) && action.equalsIgnoreCase(ACTION_ADD_DTL)) {
				processActionAddDetail(mapping, formIn, request, response, errors);
				saveToken(request);
				setHandled(request);
            } else if (!isHandled(request) && action.equalsIgnoreCase(ACTION_DEL_DTL)) {
                processActionDelDetail(mapping, formIn, request, response, errors);
                saveToken(request);
                setHandled(request);
			} else if (!isHandled(request) && action.startsWith(P_EDIT_DTL_REMARK)) {
                setHandled(request);
                actionForward = processActionPEdit(mapping, formIn, request, response, errors);
                saveToken(request);
            } else if (!isHandled(request) && action.startsWith(P_EDIT_DTL_REMARK)) {
                setHandled(request);

            }
		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}

		return actionForward;
	}

    protected ActionForward processActionDelDetail(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response, ActionErrors errors) {
        RemarkMaintenanceForm form = (RemarkMaintenanceForm) formIn;
        ActionForward actionForward = new ActionForward(mapping.getInput());
        int idx = Integer.parseInt(form.getPageEventSrc());
        RemarkDtlRow rowToEdit = (RemarkDtlRow) form.getMultiRows(TAB_IDX_DTL).get(idx);
        rowToEdit.setIsDelete("checked");
        form.setRow(idx, rowToEdit);
        return actionForward;
    }

	protected ActionForward processOtherStatus(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response,
		ActionErrors errors, String rb_status) {

		HttpSession session = request.getSession(false);
		RemarkMaintenanceForm form = (RemarkMaintenanceForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {
            if (rb_status.startsWith(STATUS_PSET)) {
                this.processStatusPSet(mapping, formIn, request, response, errors, rb_status);
            }
		} catch (Exception e) {
			Logger.log(this, e);
		}

		return actionForward;
	}

    protected ActionForward processActionPEdit(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response, ActionErrors errors) {
        RemarkMaintenanceForm form = (RemarkMaintenanceForm) formIn;
        HttpSession session = request.getSession(false);
        ActionForward actionForward = new ActionForward(mapping.getInput());
        String action = CommonUtil.nullToString(form.getPageEvent());
        LinkedList keyList = new LinkedList();
        keyList.add(form.getTs().getDisplayWebLabelByObjectId("LBL_HDR_REMARK_MASTER_CODE"));
        keyList.add(form.getRemarkMasterCode());

        String field = action.substring(ACTION_PEDIT.length());
        String eventSrc = form.getPageEventSrc();
        String fieldName = "";
        String fieldValue = "";

        if (field.equalsIgnoreCase(FIELD_DTLREMARK)) {
            int selectedTab = TAB_IDX_DTL;
            int selectedIndex = Integer.parseInt(eventSrc);
            ArrayList rows = form.getMultiRows(selectedTab);
            RemarkDtlRow row = (RemarkDtlRow) rows.get(selectedIndex);
            keyList.add(form.getChildTs()[selectedTab].getDisplayWebLabelByObjectId("LBL_DTL_REMARK_DTL_CODE"));
            keyList.add(row.getRemarkDtlCode());
            fieldName = form.getChildTs()[selectedTab].getDisplayWebLabelByObjectId("LBL_DTL_REMARK");
            fieldValue = row.getDtlRemark();
        }

        HashMap params = new HashMap();
        params.put(ProjectConstants.SESSION_SESSION_VALUE, getSessionSimpleMaintStatus());
        params.put(ProjectConstants.SESSION_OPENER_PATH, getFunctionId());
        params.put(ProjectConstants.SESSION_KEY_LIST, keyList);
        params.put(ProjectConstants.SESSION_FIELD, field);
        params.put(ProjectConstants.SESSION_EVENTSRC, eventSrc);
        params.put(ProjectConstants.SESSION_EDIT_COLUMN_NAME, fieldName);
        params.put(ProjectConstants.SESSION_EDIT_VALUE, fieldValue);
        session.setAttribute(ProjectConstants.SESSION_REMARK_TEXT_PARAMETERS, params);
        return actionForward;
    }

    protected ActionForward processStatusPSet(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response, ActionErrors errors, String rb_status) {
        RemarkMaintenanceForm form = (RemarkMaintenanceForm) formIn;
        HttpSession session = request.getSession(false);
        ActionForward actionForward = new ActionForward(mapping.getInput());

        HashMap params = (HashMap) session.getAttribute(ProjectConstants.SESSION_REMARK_TEXT_PARAMETERS);
        session.removeAttribute(ProjectConstants.SESSION_REMARK_TEXT_PARAMETERS);

        // sets the remark text
        String field = (String) params.get(ProjectConstants.SESSION_FIELD);
        String eventSrc = (String) params.get(ProjectConstants.SESSION_EVENTSRC);
        String editValue = (String) params.get(ProjectConstants.SESSION_EDIT_VALUE);

        ArrayList rows = form.getMultiRows(TAB_IDX_DTL);
        int selectedIndex = Integer.parseInt(eventSrc);
        if (rows == null || rows.size() < selectedIndex) return actionForward;
        RemarkDtlRow row = (RemarkDtlRow) rows.get(selectedIndex);
        if (field.equals(FIELD_DTLREMARK)) {
            row.setDtlRemark(editValue);
        }

        return actionForward;
    }

	protected ActionForward processActionAddDetail(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

        HttpSession session = request.getSession(false);
        RemarkMaintenanceForm form = (RemarkMaintenanceForm) formIn;
        ActionForward actionForward = new ActionForward(mapping.getInput());
        String action = CommonUtil.nullToString(form.getAction());
        User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

        try {
            RemarkDtlRow rowToAdd = new RemarkDtlRow();
            form.setCurrIdx(TAB_IDX_DTL);
            rowToAdd.setCompanyCode(form.getCompanyCode());
            rowToAdd.setRemarkMasterCode(form.getRemarkMasterCode());
            rowToAdd.setDtlDefault(false);
            rowToAdd.setDtlStatus(ProjectConstants.GENERIC_ACTIVE);
            form.addRow(rowToAdd);
        } catch (Exception e) {
        	Logger.logExceptions(this, e);
        }

        return actionForward;
	}

	//----------------------------------------------------------------------------//
	//add your new business/logical methods/functions HERE
	//----------------------------------------------------------------------------//


	////////////////////////////////////////////////////////////////////////////////
	// END OF LOGICAL AND BUSINESS METHODS
	////////////////////////////////////////////////////////////////////////////////


	////////////////////////////////////////////////////////////////////////////////
	// OVERRIDES PARENT'S OPTIONAL METHODS
	////////////////////////////////////////////////////////////////////////////////

	protected void initAction(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		super.initAction(mapping, formIn, request, response, errors);

		HttpSession session = request.getSession(false);
		RemarkMaintenanceForm form = (RemarkMaintenanceForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void initForm(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		super.initForm(mapping, formIn, request, response, errors);

		HttpSession session = request.getSession(false);
		RemarkMaintenanceForm form = (RemarkMaintenanceForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void initPage(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		super.initPage(mapping, formIn, request, response, errors);

		HttpSession session = request.getSession(false);
		RemarkMaintenanceForm form = (RemarkMaintenanceForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void beforeCreate(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		RemarkMaintenanceForm form = (RemarkMaintenanceForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {
			form.setCompanyCode(WebUtil.getAppropriateCompanyCode(session, ProjectConstants.SP_SHARE_REMARK));
			form.setStatus(ProjectConstants.GENERIC_ACTIVE);
			form.setTs(new RemarkHdrSchema());
			form.setTsName("com.erp.app.setup.remark.schema.RemarkHdrSchema");
			form.setDcInd(GENERIC_NO);
		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void beforeSave(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		RemarkMaintenanceForm form = (RemarkMaintenanceForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void afterSave(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		RemarkMaintenanceForm form = (RemarkMaintenanceForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void beforeEdit(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		RemarkMaintenanceForm form = (RemarkMaintenanceForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void afterEdit(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		RemarkMaintenanceForm form = (RemarkMaintenanceForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void beforeDelete(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		RemarkMaintenanceForm form = (RemarkMaintenanceForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void afterDelete(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		RemarkMaintenanceForm form = (RemarkMaintenanceForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void beforeRefresh(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		RemarkMaintenanceForm form = (RemarkMaintenanceForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void afterRefresh(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		RemarkMaintenanceForm form = (RemarkMaintenanceForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	public String getSessionMaintStatus() {

		return getSessionSimpleMaintStatus();
	}

	public String getSessionMaintPk() {

		return getSessionSimpleMaintPk();
	}

	public String getStrutsId() {

		return getFunctionId();
	}

	protected String getSimpleSrchMenuId() {
		return ProjectConstants.MOD_REMARK;
	}

	////////////////////////////////////////////////////////////////////////////////
	// END OF OPTIONAL METHODS
	////////////////////////////////////////////////////////////////////////////////


	////////////////////////////////////////////////////////////////////////////////
	// IMPLEMENTS PARENT'S ABSTRACT METHODS
	////////////////////////////////////////////////////////////////////////////////

	protected String getModuleName(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request,HttpServletResponse response) {
		return ProjectConstants.MOD_REMARK;
	}

	protected String getFunctionId() {

		//struts-config xxxxx.do
		return "remarkMaintenance.do";
	}

	protected Class getMasterFormClass() {

		//the corresponding form class
		return RemarkMaintenanceForm.class;
	}

	protected Class getMasterVOClass() {

		//corresponding master vo class
		return RemarkHdrVo.class;
	}

	protected String getMasterFormSessionName() {

		return "remarkMaintenanceForm";
	}

	protected Class[] getChildRowClass() {

		Class[] childRowClass = new Class[1];

		childRowClass[0] = com.erp.app.setup.remark.web.RemarkDtlRow.class;

		return childRowClass;
	}

	protected String[] getFolderChildIdxMapping() {

		String[] folderChildIdx = new String[2];

		folderChildIdx[0] = String.valueOf(TAB_IDX_DTL);
		folderChildIdx[1] = String.valueOf(TAB_IDX_DTL);

		return folderChildIdx;
	}

	protected SimpleBasePk getPk(ActionForm formIn, HttpServletRequest request) {

		RemarkMaintenanceForm form = (RemarkMaintenanceForm) formIn;
		RemarkHdrPk pk = null;
		HttpSession session = request.getSession(false);

		String companyCode = WebUtil.getAppropriateCompanyCode(session, ProjectConstants.SP_SHARE_REMARK);

//		edit the following with appropriate form field names
		pk = new RemarkHdrPk(companyCode, form.getRemarkMasterCode());
		return pk;
	}

	protected void setFormType(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
		HttpServletResponse response, ActionErrors errors){

		RemarkMaintenanceForm form = (RemarkMaintenanceForm) formIn;
		form.setFormType(ProjectConstants.FORM_TYPE_MAINT);
	}

	////////////////////////////////////////////////////////////////////////////////
	// END OF ABSTRACT METHODS
	////////////////////////////////////////////////////////////////////////////////
}
